//
//  RegistViewModel.m
//  AppleMarket
//
//  Created by yangy on 25/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "RegistViewModel.h"

#import "ImageCodeMoel.h"

@implementation RegistViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        
        [self.refershSMSCodeCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [SVProgressHUD showInfoWithStatus:@"发送成功"];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        [[self.refershSMSCodeCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
        [self.reigisterCommond.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                [SVProgressHUD showInfoWithStatus:@"恭喜您，注册成功"];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        [[self.reigisterCommond.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
    }
    return self;
}

- (RACCommand *)refershSMSCodeCommand
{
    if(!_refershSMSCodeCommand){
        _refershSMSCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACTupleUnpack(NSString *key,NSString *inputCode) = input;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 发送短信验证码
                [NetRequest request_SendMsgCodeType:@"Signup"
                                            account:self.accountStr_
                                               code:inputCode
                                                key:key
                                    completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _refershSMSCodeCommand;
}

- (RACCommand *)reigisterCommond
{
    if(!_reigisterCommond){
        _reigisterCommond = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                RACTupleUnpack(NSString *key,NSString *inputCode) = input;
                [NetRequest request_registerAccount:self.accountStr_ verifyCode:self.codeStr_ agree:YES password:self.passwordStr_ imgCode:inputCode imgKey:key completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _reigisterCommond;
}

- (RACSubject *)sendCodeCkickSubject
{
    if(!_sendCodeCkickSubject){
        _sendCodeCkickSubject = [RACSubject subject];
    }
    return _sendCodeCkickSubject;
}

- (RACSubject *)registClickSubject{
    if(!_registClickSubject){
        _registClickSubject = [RACSubject subject];
    }
    return _registClickSubject;
}

- (RACSubject *)userAgreeClickSubject
{
    if(!_userAgreeClickSubject){
        _userAgreeClickSubject = [RACSubject subject];
    }
    return _userAgreeClickSubject;
}


@end
